package xyz.robinbingo.leetcode.a20.b06.c21;

import xyz.robinbingo.leetcode.TreeNode;

public class Solution {
    int max = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        max(root);
        return max;
    }

    private int max(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int i = node.val;
        int l = Math.max(0, max(node.left));
        int r = Math.max(0, max(node.right));
        max = Math.max(i + l + r, max);
        return i + Math.max(l, r);
    }
}